package com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.viewholder;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.PicturePreviewActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.ImageViewAdp3;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.RadioStationAdapter;
import com.ninetaildemonfox.zdl.socialproject.utils.GlideUtils;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.CommentActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/7/19
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class DynameicAdViewHolder extends BaseViewHolder<Map<String, String>> {
    @BindView(R.id.text_name)
    TextView textName;
    @BindView(R.id.text_identification)
    TextView textIdentification;
    @BindView(R.id.text_remarks)
    TextView textRemarks;
    @BindView(R.id.text_comment)
    TextView textComment;
    @BindView(R.id.text_create_time)
    TextView textCreateTime;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.reitem)
    RecyclerView reitem;
    @BindView(R.id.text_good_count)
    TextView textGoodCount;
    @BindView(R.id.image_head_pic)
    ImageView image_head_pic;
    private int count;

    public DynameicAdViewHolder(View itemView, int count) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        this.count = count;
    }

    @Override
    public void setData(final Map<String, String> data) {
        super.setData(data);
        GlideUtils.glideFille(getContext(), data.get("head_pic"), image_head_pic);
        textName.setText(data.get("nickname"));
        String identification = data.get("identification");


        if (data.get("sex").equals("1")) {
            if (data.get("vip").equals("1")) {
                textIdentification.setText("非会员");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
            } else {
                textIdentification.setText("会员");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
            }

        } else {
            if (identification.equals("1")) {
                textIdentification.setText("未认证");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.text_black));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg_false);
            } else {
                textIdentification.setText("已认证");
                textIdentification.setTextColor(getContext().getResources().getColor(R.color.white));
                textIdentification.setBackgroundResource(R.drawable.shape_drawable_my_text_bg);
            }
        }
        Drawable drawable2;
        //1可以评论
        if (data.get("can_comment").equals("1")) {
            drawable2 = getContext().getResources().getDrawable(R.mipmap.ic_comment);
            drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        } else {
            drawable2 = getContext().getResources().getDrawable(R.mipmap.ic_comment_no);
            drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
        }
        textComment.setCompoundDrawables(null, drawable2, null, null);

        textCreateTime.setText("发布于" + data.get("create_time"));
        List<Map<String, String>> list = new ArrayList<>();
        ArrayList<Map<String, String>> list1 = JSONUtils.parseKeyAndValueToMapList(data.get("pics"));
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 4));
        ImageViewAdp3 imagViewAdapter = new ImageViewAdp3(R.layout.imageview3);
        imagViewAdapter.setCount(1);
        recyclerView.setAdapter(imagViewAdapter);
        if (list1 == null || list1.size() == 0) {
            recyclerView.setVisibility(View.GONE);
        } else {
            imagViewAdapter.setNewData(list1);
            recyclerView.setVisibility(View.VISIBLE);
        }
        imagViewAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data1 = imagViewAdapter.getData();
                PicturePreviewActivity.start(getContext(), data1, position, 2, "");
            }
        });

        //点赞数量
        textGoodCount.setText(data.get("good_count"));
        Drawable drawable = null;
        if (data.get("good_count").equals("0") || TextUtils.isEmpty(data.get("good_count"))) {
            drawable = getContext().getResources().getDrawable(R.mipmap.ic_give_the_thumbs_up_false);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        } else {
            if (data.get("good").equals("2")){
                drawable = getContext().getResources().getDrawable(R.mipmap.ic_give_the_thumbs_up_true);
            }else {
                drawable = getContext().getResources().getDrawable(R.mipmap.ic_give_the_thumbs_up_false);
            }
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        }
        textGoodCount.setCompoundDrawables(null, drawable, null, null);
        //备注
        textRemarks.setText(data.get("remarks"));
        //评论
        textComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (data.get("can_comment").equals("1")) {
                    CommentActivity.start(getContext(), data.get("id"));
                } else {
                    ToastUtil.show("禁止评论");
                }
            }
        });
        //点赞
        textGoodCount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemOnClick != null) {
                    itemOnClick.onClick(getAdapterPosition(), data, 1);
                }
            }
        });
    }

    public interface ItemOnClick {
        void onClick(int position, Map<String, String> data, int type);
    }

    private ItemOnClick itemOnClick;

    public void setmOnClick(ItemOnClick mOnClick) {
        this.itemOnClick = mOnClick;
    }

}
